package com.lzs.kunei.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.*;
import com.lzs.kucun.service.StockService;
import com.lzs.kunei.service.InventoryLocatorDetailService;
import com.lzs.kunei.service.InventoryLocatorService;
import com.lzs.util.JwtUtils;
import com.lzs.util.R;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sun.awt.SunHints;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpy
 * @since 2020-11-24
 */
@ApiModel(value = "盘点货位")
@RestController
@RequestMapping("/kunei/inventorylocator")
public class InventoryLocatorController {
    @Autowired
    private InventoryLocatorService inventoryLocatorService;
    @Autowired
    private StockService stockService;
    @Autowired
    private InventoryLocatorDetailService inventoryLocatorDetailService;

    @ApiOperation(value = "盘点单中添加盘点货位")
    @PostMapping("/addLocator")
    public R addLocator(@RequestBody InventoryLocator inventoryLocator,HttpServletRequest request){
        try {
            String token=request.getHeader("token");
            Long userid= JwtUtils.getMemberIdByJwtToken(token);
            User user=stockService.selectUser(userid);
            String name = user.getName();
            String locatorcode = inventoryLocator.getLocatorcode();
            Locator locator = stockService.selectLocator(locatorcode);
            inventoryLocator.setLocatorid(locator.getId());
            inventoryLocator.setLocatorname(locator.getName());
            inventoryLocator.setCreateid(userid);
            inventoryLocator.setCreator(name);
            inventoryLocatorService.save(inventoryLocator);
            return R.ok().message("添加盘点单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            return R.error().message("添加盘点单失败");
        }

    }

    @ApiOperation(value = "添加货位下拉")
    @PostMapping("/selectLocatorcode")
    public R selectLocatorcode(@RequestBody Inventory inventory){
        try {
        Long warehouseid = inventory.getWarehouseid();
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("warehouseid",warehouseid);
        List<Stock> listLocatorcode = stockService.list(queryWrapper);
       return R.ok().message("添加货位下拉成功").data("Locatorcode",listLocatorcode);
    }catch (RuntimeException e){
            e.printStackTrace();
            return R.error().message("添加货位下拉失败");
        }

    }

    @ApiOperation(value = "查询盘点货位")
    @PostMapping("/selectInventoryLocatorPage/{current}/{size}")
    public R selectPutonPage2(@PathVariable Integer current, @PathVariable Integer size, @RequestBody InventoryLocator inventoryLocator,HttpServletRequest request){
        String warehouseId = request.getHeader("warehouseId");
        String[] warehouseid = warehouseId.split(",");
        Page<InventoryLocator> page=new Page<>(current,size);
        String inventoryno = inventoryLocator.getInventoryno();
        String locatorcode = inventoryLocator.getLocatorcode();
        QueryWrapper<InventoryLocator> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("warehouseid",warehouseid);
        if (StringUtils.isNotEmpty(inventoryno)) {
            queryWrapper.like("inventoryno", inventoryno);
        }
        if (StringUtils.isNotEmpty(locatorcode)){
        queryWrapper.like("locatorcode",locatorcode);
        }
        Page<InventoryLocator> page1 = inventoryLocatorService.page(page, queryWrapper);
        return R.ok().message("查询成功").data("inventoryLocatorPage",page1);
    }

    @ApiOperation(value = "查询商品信息下拉")
    @PostMapping("/selectCommoditybar")
    public R selectCommodity(@RequestBody InventoryLocatorDetail inventoryLocatorDetail){
        String inventoryno = inventoryLocatorDetail.getInventoryno();
        String locatorcode = inventoryLocatorDetail.getLocatorcode();
        QueryWrapper<InventoryLocator> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("inventoryno",inventoryno);
        Long warehouseid = inventoryLocatorService.list(queryWrapper).get(0).getWarehouseid();
        List<Stock>  commoditybars=(List<Stock>)stockService.selectCommoditybar(locatorcode, warehouseid).getData().get("commoditybars");

        return R.ok().data("commoditybars",commoditybars);
    }
    @ApiOperation(value = "删除盘点单")
    @PostMapping("/deleteInventory")
    public R deleteInventory(@RequestBody InventoryLocator inventoryLocator){
        try {
            String inventoryno = inventoryLocator.getInventoryno();
            QueryWrapper<InventoryLocator> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("inventoryno", inventoryno);
            inventoryLocatorService.remove(queryWrapper);
            QueryWrapper<InventoryLocatorDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("inventoryno", inventoryno);
            inventoryLocatorDetailService.remove(queryWrapper1);
            return R.ok().message("删除成功");
        }catch (RuntimeException e){
            return R.error().message("删除失败");
        }

    }





}

